using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;

namespace TST.SharePoint.Shared
{
    /// <summary>
    /// Abstract class for an editor part that uses custom properties.
    /// Used by editor parts for datasources that have config options specific for that datasource.
    /// </summary>
    public abstract class CustomPropertyEditor : Control
    {
        protected const string DOTTEDLINE = "<div style='width:100%' class='UserDottedLine'></div>";
        private GridDatasource _datasource;
        private DatasourceConfiguration _configuration;
        private string _webpartId;
        protected Exception _exception;

        /// <summary>
        /// Contructor. Make sure the ID is set, to prevent the page from crashing in SharePoint.
        /// </summary>
        public CustomPropertyEditor(GridDatasource datasource, DatasourceConfiguration configuration, string webPartId)
        {
            this.ID = "CustomPropertyEditor";
            _datasource = datasource;
            _configuration = configuration;
            _webpartId = webPartId;
            EnableViewState = true;
        }

        /// <summary>
        /// To be implemented by the editor part. Write the values to the custom property list.
        /// </summary>
        /// <param name="properties"></param>
        public abstract void WriteConfiguration();

        /// <summary>
        /// To be implemented by the editor part. Read the values from the custom properties list and
        /// set the values in the editor controls.
        /// </summary>
        /// <param name="properties"></param>
        public abstract void ReadConfiguration();

        protected GridDatasource Datasource
        {
            get { return _datasource; }
        }

        protected DatasourceConfiguration Configuration
        {
            get { return _configuration; }
        }

        /// <summary>
        /// Get or set the current Configuration. Saved in the ViewState.
        /// </summary>
        protected DatasourceConfiguration CurrentConfiguration
        {
            get
            {
                if (ViewState[ViewStateName] == null)
                {
                    DatasourceConfiguration c;
                    if (Configuration != null)
                        c = Configuration;
                    else
                        c = new DatasourceConfiguration();
                    ViewState[ViewStateName] = c;
                    return c;
                }
                else
                {
                    return (DatasourceConfiguration)ViewState[ViewStateName];
                }
            }
            set
            {
                ViewState[ViewStateName] = value;
            }
        }

        private string ViewStateName
        {
            get
            {
                return "EditorCurrentConfiguration" + _webpartId;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            if (_exception != null)
            {
                writer.Write(string.Format("Error: {0}", _exception.Message));
            }
        }


    }
}
